<?php
/**
 * data adaptor for mysql
 * Actually uses PDO...
 * @author bbqq <toughgamer@gmail.com>
 * @package com.blib.data
 * @copyright bbqq qstufie.com
 * @implements <Blib_Interface_Data_Adaptor>
 */
class Blib_Data_Adaptor_Mysql extends Blib_Data_Adaptor_Pdo
{
    /**
     * construct the adaptor
     *
     * @param array $options
     *
     * @return void
     */
    public function __construct($options = array())
    {
        if (!isset($options['host']) ||
            !isset($options['user']) ||
            !isset($options['pass']) ||
            !isset($options['dbname'])) {
            return $this->_error(null, null, 'Option is invalid');
        }
        $dsn = "mysql:dbname={$options['dbname']};host={$options['host']}";
        if (isset($options['port']) && !empty($options['port'])) {
            $dsn .= ':' . $options['port'];
        }

        // we dont want more than 1 connection to the db...
        $key = md5($dsn . $options['user'] . $options['pass']);
        if (Blib::get($key) instanceof Blib_Data_Dao_Wrapper_Pdo) {
            $this->_pdo = Blib::get($key);
            $this->_pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, TRUE);
            return true;
        }

        try {
            Blib::register($key, new Blib_Data_Dao_Wrapper_Pdo($dsn, $options['user'], $options['pass']));
            $this->_pdo = Blib::get($key);
            $this->_pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, TRUE);
            // enable pdo buffering.
            $this->_pdo->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, TRUE);
        } catch (PDOException $e) {
            // throw our own exception object
            throw new Blib_Exception_Dao(null, null, $e);
        }

    }// end __construct

    // quote names
    public function nameQuote($name)
    {
        if (strpos($name, '.') !== false) {
            $name = str_replace('.', '`,`',  $object->__getTableName());
        }
        return "`$name`";

    }// end nameQuote

}// end Blib_Data_Adaptor_Mysql
?>